<template>
  <div class="app-container">
    <div class="filter-container">
      <el-tabs v-model="activeName">
        <!-- <el-tab-pane label="所有任务" name="first">
         <allprocess></allprocess>
        </el-tab-pane> -->
        <el-tab-pane label="待审核" name="first"><ysc></ysc></el-tab-pane>
        <!-- <el-tab-pane label="已审核" name="third"><ysh></ysh></el-tab-pane> -->
        <el-tab-pane label="通过审核" name="second"><yiruku></yiruku></el-tab-pane
        >
      </el-tabs>
      <!-- <el-button v-waves :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">
        {{ $t('table.export') }}
      </el-button> -->
      <!-- <el-checkbox v-model="showReviewer" class="filter-item" style="margin-left:15px;" @change="tableKey=tableKey+1">
        {{ $t('table.reviewer') }}
      </el-checkbox> -->
    </div>
  </div>
</template>

<script>
import { createUser, deletUser, getUserList, updateUser } from '@/api/user'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import Ysh from './ysh.vue'
import Yiruku from './yiruku.vue'
import Ysc from './ysc.vue'
import Allprocess from './allprocess.vue'

export default {
  directives: { waves },
  components: { Pagination ,Ysh, Yiruku,Ysc,Allprocess},
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      activeName: 'first',
      dialogFormVisible: false,
      dialogStatus: '',
      sortOptions: [
        { label: 'ID Ascending', key: '+id' },
        { label: 'ID Descending', key: '-id' }
      ],
      rules: {
        type: [
          { required: true, message: 'type is required', trigger: 'change' }
        ],
        timestamp: [
          {
            type: 'date',
            required: true,
            message: 'timestamp is required',
            trigger: 'change'
          }
        ],
        title: [
          { required: true, message: 'title is required', trigger: 'blur' }
        ]
      },
      textMap: {
        update: '编辑',
        create: '新建'
      },
      temp: {
        id: '',
        username: '',
        password: '',
        type: '',
        phone: '',
        rname: ''
      },
      listQuery: {
        page: 1,
        limit: 20,
        username: undefined,
        type: undefined,
        sort: '+id'
      },
      statusOptions: ['系统管理员', '档案管理员', '资料管理员', '普通用户']
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getUserList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    resetTemp() {
      this.temp = {
        id: '',
        username: '',
        password: this.list.password,
        type: '',
        phone: '',
        rname: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          // this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
          // this.temp.author = 'vue-element-admin'
          // this.temp.timestamp = +new Date(this.temp.timestamp)
          console.log(this.temp)
          createUser(this.temp).then(() => {
            this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
            location.reload()
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          //   tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
          updateUser(tempData).then(() => {
            const index = this.list.findIndex(v => v.id === this.temp.id)
            this.list.splice(index, 1, this.temp)
            console.log(this.list)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            location.reload()
          })
        }
      })
    },
    handleDelete(row, index) {
      deletUser(row.id).then(() => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        const index = this.list.findIndex(v => v.id === this.temp.id)
        this.list.splice(index, 1)
      })
    }
  }
}
</script>
